from fastapi import APIRouter, Depends
from crud.scales import scaledetail as scaledetail_crud
from common import exception, response_code
import models
from dependencies import SessionDep
from datetime import datetime
from typing import List, Annotated
from routers.auth import get_current_sys_user, get_current_patient_user, get_current_doctor_user

router = APIRouter()

"""获取量表详情
Keyword arguments:
scale_detail -- 量表id
status -- 状态
session -- 数据库会话
user -- 当前用户
Return: response -- 响应体
"""


@router.get(
    "/api/scale/detail/{scale_id}",
    tags=["量表详情管理"],
    summary="获取量表详情",
    response_model=models.RespJsonScaleDetail,
)
async def get_scale(
    scale_id: int,
    status: int = 0,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    response: models.RespJsonScaleDetail = Depends(
        response_code.RespSuccessJson.resp_200
    ),
):
    # 查询量表详情数据
    scale_detail = scaledetail_crud.get_scale_detail(
        scale_id, status, user
    )
    # 响应体
    response.data = scale_detail

    return response


"""编辑/保存量表详情

Keyword arguments:
scale_detail -- 编辑/保存量表详情数据
user -- 当前用户
Return: response -- 响应体
"""


@router.post(
    "/api/scale/detail/",
    tags=["量表详情管理"],
    summary="编辑/保存量表详情",
    response_model=models.RespJsonScaleDetail,
)
async def update_scale_detail(
    session: SessionDep,
    scale_detail: models.ScaleDetailUpdate,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    response: models.RespJsonScaleDetail = Depends(
        response_code.RespSuccessJson.resp_200
    ),
):
    # 暂存量表详情
    scaledetail_crud.update_scale_detail(scale_detail, user)

    # 获取量表基础数据
    scale_detail = session.get(models.ScaleDetail, scale_detail.id)
    scale_id = scale_detail.scale_id
    scale = session.get(models.Scale, scale_id)

    # 更新更新人和更新时间
    scale.update_id = user.id
    scale.updatetime = datetime.now()
    session.add(scale)
    session.commit()

    # 响应体
    response.msg = "保存成功"
    return response


"""查询量表评分规则

Keyword arguments:
scale_detail_id -- 量表详情id
user -- 当前用户
Return: response -- 响应体
"""


@router.get(
    "/api/scale/detail/rule/{scale_detail_id}",
    tags=["量表详情管理"],
    summary="查询量表评分规则",
    response_model=models.RespJsonScaleScoreRule,
)
async def get_scale_score_rule(
    scale_detail_id: int,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    response: models.RespJsonScaleScoreRule = Depends(
        response_code.RespSuccessJson.resp_200
    ),
):
    # 查询量表评分规则
    scaledetailrule = scaledetail_crud.get_scale_score_rule(
        scale_detail_id
    )

    # 响应体
    response.data = scaledetailrule

    return response


"""发布量表或发布量表并保存量表评分规则

Keyword arguments:
scale_detail_id -- 量表详情id
scale_score_rules -- 量表评分规则
user -- 当前用户
Return: response -- 响应体
"""


@router.post(
    "/api/scale/detail/publish/{scale_detail_id}",
    tags=["量表详情管理"],
    summary="发布量表或发布量表并保存量表评分规则",
    response_model=models.RespJsonScaleDetail,
)
async def publish_scale_detail(
    session: SessionDep,
    scale_detail_id: int,
    scale_score_rules: List[models.ScaleScoreRuleUpdate] = None,
    user: Annotated[models.SystemUser, Depends(get_current_sys_user)] = None,
    response: models.RespJsonScaleDetail = Depends(
        response_code.RespSuccessJson.resp_200
    ),
):
    # 发布量表量表详情
    scale_detail = scaledetail_crud.publish_scale_detail(
        scale_detail_id, scale_score_rules, user)
    
    # 响应体
    response.msg = "发布成功"

    return response
